//
//  HomeReviewsView.m
//  JiaoX
//
//  Created by yinqy on 15/1/5.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeReviewsView.h"
#define SUBTITLE_FONT_SIZE [DimensUtils getScaleX:12]
#define TITLE_FONT_SIZE [DimensUtils getScaleX:14]
#define READ_FONT_SIZE [DimensUtils getScaleX:10]


@interface HomeReviewsView()
/** 图 */
@property (nonatomic, weak) UIImageView *imageView;
/** 标题 */
@property (nonatomic, weak) UILabel *titleLabel;
/** 副标题 */
@property (nonatomic, weak) UILabel *subTitleLabel;

/** 回复数 */
@property (nonatomic, weak) UIButton *replyNumBtn;


@end

@implementation HomeReviewsView

-(instancetype)initWithWidth:(CGFloat)width{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, width, HomeReviewsViewHeight);
        //1.图片
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.frame = CGRectMake(JXPublicInset, JXPublicInset, HomeReviewsViewHeight-JXPublicInset*2, HomeReviewsViewHeight-JXPublicInset*2);
        [self addSubview:imageView];
        //设置layer
        CALayer *layer=[imageView layer];
        //设置边框线的宽
        [layer setBorderWidth:0.5];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
        self.imageView = imageView;
        
        //2.标题
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.textColor = JXColor(51, 51, 51);
        titleLabel.numberOfLines = 2;
        titleLabel.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
        CGFloat titleLabelX = CGRectGetMaxX(imageView.frame)+JXPublicInset;
        titleLabel.frame = CGRectMake(titleLabelX, CGRectGetMinY(imageView.frame)+[DimensUtils getScaleX:JXPublicInset/3], self.width-titleLabelX-JXPublicInset, titleLabel.font.lineHeight*2);
        _titleLabel = titleLabel;
        [self addSubview:titleLabel];
        
        //3.副标题
        
        UILabel *subTitleLabel = [[UILabel alloc]init];
        subTitleLabel.textColor = JXColor(102, 102, 102);
        subTitleLabel.font = [UIFont systemFontOfSize:SUBTITLE_FONT_SIZE];
        subTitleLabel.frame = CGRectMake(titleLabelX, CGRectGetMaxY(titleLabel.frame)+[DimensUtils getScaleX:JXPublicInset/2], titleLabel.width, subTitleLabel.font.lineHeight*2);
        subTitleLabel.numberOfLines = 2;
        
        _subTitleLabel = subTitleLabel;
        [self addSubview:subTitleLabel];
        
        //4.阅读数
        UIButton *readButton = [[UIButton alloc]init];
        [readButton.titleLabel setFont:[UIFont systemFontOfSize:READ_FONT_SIZE]];
        [readButton setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
        readButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [readButton setImage:[UIImage imageNamed:@"readNum_icon"] forState:UIControlStateNormal];
        readButton.frame = CGRectMake(titleLabelX, CGRectGetMaxY(imageView.frame)-READ_FONT_SIZE-[DimensUtils getScaleX:JXPublicInset/2], titleLabel.width*0.5, readButton.imageView.image.size.height);
        _readNumBtn = readButton;
        readButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self addSubview:readButton];
        
        //5.评论数
        UIButton *replyBtn = [[UIButton alloc]init];
        replyBtn.titleLabel.font = [UIFont systemFontOfSize:READ_FONT_SIZE];
        [replyBtn setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
        [replyBtn setImage:[UIImage imageNamed:@"replyNum_icon"] forState:UIControlStateNormal];
        replyBtn.frame = CGRectMake(CGRectGetMaxX(readButton.frame), readButton.y, titleLabel.width*0.5, replyBtn.imageView.image.size.height);
        replyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [replyBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        [self addSubview:replyBtn];
        _replyNumBtn = replyBtn;
    }
    return self;
}


//- (id)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        //1.图片
//        UIImageView *imageView = [[UIImageView alloc]init];
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
//        imageView.frame = CGRectMake(JXPublicInset, JXPublicInset, HomeReviewsViewHeight-JXPublicInset*2, HomeReviewsViewHeight-JXPublicInset*2);
//        [self addSubview:imageView];
//        //设置layer
//        CALayer *layer=[imageView layer];
//        //设置边框线的宽
//        [layer setBorderWidth:0.5];
//        //设置边框线的颜色
//        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
//        self.imageView = imageView;
//        
//        //2.标题
//        UILabel *titleLabel = [[UILabel alloc]init];
//        titleLabel.textColor = JXColor(51, 51, 51);
//        titleLabel.numberOfLines = 2;
//        titleLabel.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
//        CGFloat titleLabelX = CGRectGetMaxX(imageView.frame)+JXPublicInset;
//        titleLabel.frame = CGRectMake(titleLabelX, CGRectGetMinY(imageView.frame)+[DimensUtils getScaleX:JXPublicInset/3], JXScreenW-titleLabelX-JXPublicInset, titleLabel.font.lineHeight*2);
//        _titleLabel = titleLabel;
//        [self addSubview:titleLabel];
//        
//        //3.副标题
//        
//        UILabel *subTitleLabel = [[UILabel alloc]init];
//        subTitleLabel.textColor = JXColor(102, 102, 102);
//        subTitleLabel.font = [UIFont systemFontOfSize:SUBTITLE_FONT_SIZE];
//        subTitleLabel.frame = CGRectMake(titleLabelX, CGRectGetMaxY(titleLabel.frame)+[DimensUtils getScaleX:JXPublicInset/2], titleLabel.width, subTitleLabel.font.lineHeight*2);
//        subTitleLabel.numberOfLines = 2;
//        
//        _subTitleLabel = subTitleLabel;
//        [self addSubview:subTitleLabel];
//        
//        //4.阅读数
//        UIButton *readButton = [[UIButton alloc]init];
//        [readButton.titleLabel setFont:[UIFont systemFontOfSize:READ_FONT_SIZE]];
//        [readButton setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
//        readButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
//        [readButton setImage:[UIImage imageNamed:@"readNum_icon"] forState:UIControlStateNormal];
//        readButton.frame = CGRectMake(titleLabelX, CGRectGetMaxY(imageView.frame)-READ_FONT_SIZE-[DimensUtils getScaleX:JXPublicInset/2], titleLabel.width*0.5, readButton.imageView.image.size.height);
//        _readNumBtn = readButton;
//        readButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        [self addSubview:readButton];
//        
//        //5.评论数
//        UIButton *replyBtn = [[UIButton alloc]init];
//        replyBtn.titleLabel.font = [UIFont systemFontOfSize:READ_FONT_SIZE];
//        [replyBtn setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
//        [replyBtn setImage:[UIImage imageNamed:@"replyNum_icon"] forState:UIControlStateNormal];
//        replyBtn.frame = CGRectMake(CGRectGetMaxX(readButton.frame), readButton.y, titleLabel.width*0.5, replyBtn.imageView.image.size.height);
//        replyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//        [replyBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
//        [self addSubview:replyBtn];
//        _replyNumBtn = replyBtn;
//        
//        self.frame = CGRectMake(0, 0, JXScreenW, HomeReviewsViewHeight);
//    }
//    return self;
//}

-(void)setReviews:(HomeReviews *)reviews{
    _reviews = reviews;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:reviews.img]];
    
    self.titleLabel.height = self.titleLabel.font.lineHeight*2;
    self.subTitleLabel.height = self.subTitleLabel.font.lineHeight*2;
    
    CGSize titleSize = [reviews.title sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(self.titleLabel.width, self.titleLabel.height)];
    self.titleLabel.size = titleSize;
    self.titleLabel.text = reviews.title;
    
    CGSize subTitleSize = [reviews.content sizeWithFont:self.subTitleLabel.font constrainedToSize:CGSizeMake(self.subTitleLabel.width, self.subTitleLabel.height)];
    self.subTitleLabel.size = subTitleSize;
    self.subTitleLabel.text = reviews.content;
    self.subTitleLabel.y = CGRectGetMaxY(self.titleLabel.frame)+(CGRectGetMinY(self.readNumBtn.frame)-CGRectGetMaxY(self.titleLabel.frame)-subTitleSize.height)/2;
    
    if (!ISBLANK(reviews.nickName)) {
        [self.readNumBtn setTitle:reviews.nickName forState:UIControlStateNormal];
        NSString *genderImg = @"ic_gender_small_unknow";
        //判断是男是女
        if (reviews.userGender==1) {
            genderImg = @"ic_gender_small_man";
        }else if(reviews.userGender==2){
            genderImg = @"ic_gender_small_woman";
        }
        //判断是否是vip
        if (reviews.level) {
            genderImg = [NSString stringWithFormat:@"%@_vip",genderImg];
        }
        [self.readNumBtn setImage:IMG(genderImg) forState:UIControlStateNormal];
    }else{
        [self.readNumBtn setImage:[UIImage imageNamed:@"readNum_icon"] forState:UIControlStateNormal];
        [self.readNumBtn setTitle:[NSString stringWithFormat:@"%@",reviews.readNum] forState:UIControlStateNormal];
    }

    [self.replyNumBtn setTitle:[NSString stringWithFormat:@"%@",reviews.replyNum] forState:UIControlStateNormal];
}

@end
